#!/usr/bin/make -f

VERSION ?= $(shell cat engine/VERSION)

override_dh_auto_build:
	# Build the daemon and dependencies
	cd engine && DOCKER_GITCOMMIT=$(ENGINE_GITCOMMIT) PRODUCT=docker ./hack/make.sh dynbinary
	cd engine && TMP_GOPATH="/go" hack/dockerfile/install/install.sh tini
	cd engine && TMP_GOPATH="/go" hack/dockerfile/install/install.sh proxy dynamic
	cd engine && TMP_GOPATH="/go" hack/dockerfile/install/install.sh rootlesskit dynamic
	# Build the CLI
	cd /go/src/github.com/docker/cli && \
		LDFLAGS='' DISABLE_WARN_OUTSIDE_CONTAINER=1 make VERSION=$(VERSION) GITCOMMIT=$(CLI_GITCOMMIT) dynbinary manpages
	# Build the CLI plugins
	# Make sure to set LDFLAGS="" since, dpkg-buildflags sets it to some weird values
	set -e;cd /sources && \
		tar xzf plugin-installers.tgz; \
		for installer in plugins/*.installer; do \
			LDFLAGS='' bash $${installer} build; \
		done

override_dh_auto_test:
	./engine/bundles/dynbinary-daemon/dockerd -v
	./cli/build/docker -v

override_dh_strip:
	# Go has lots of problems with stripping, so just don't

override_dh_auto_install:
	# docker-ce-cli install
	install -D -m 0644 /go/src/github.com/docker/cli/contrib/completion/fish/docker.fish debian/docker-ce-cli/usr/share/fish/vendor_completions.d/docker.fish
	install -D -m 0644 /go/src/github.com/docker/cli/contrib/completion/zsh/_docker debian/docker-ce-cli/usr/share/zsh/vendor-completions/_docker
	install -D -m 0755 /go/src/github.com/docker/cli/build/docker debian/docker-ce-cli/usr/bin/docker
	set -e;cd /sources && \
		tar xzf plugin-installers.tgz; \
		for installer in plugins/*.installer; do \
			DESTDIR=/root/build-deb/debian/docker-ce-cli \
			PREFIX=/usr/libexec/docker/cli-plugins \
				bash $${installer} install_plugin; \
		done
	# docker-ce install
	install -D -m 0644 /sources/docker.service debian/docker-ce/lib/systemd/system/docker.service
	install -D -m 0644 /sources/docker.socket debian/docker-ce/lib/systemd/system/docker.socket
	install -D -m 0755 $(shell readlink -e engine/bundles/dynbinary-daemon/dockerd) debian/docker-ce/usr/bin/dockerd
	install -D -m 0755 /usr/local/bin/docker-proxy debian/docker-ce/usr/bin/docker-proxy
	install -D -m 0755 /usr/local/bin/docker-init debian/docker-ce/usr/bin/docker-init

	# docker-ce-rootless-extras install
	install -D -m 0755 /usr/local/bin/rootlesskit debian/docker-ce-rootless-extras/usr/bin/rootlesskit
	install -D -m 0755 /usr/local/bin/rootlesskit-docker-proxy debian/docker-ce-rootless-extras/usr/bin/rootlesskit-docker-proxy
	install -D -m 0755 engine/contrib/dockerd-rootless.sh debian/docker-ce-rootless-extras/usr/bin/dockerd-rootless.sh
	install -D -m 0755 engine/contrib/dockerd-rootless-setuptool.sh debian/docker-ce-rootless-extras/usr/bin/dockerd-rootless-setuptool.sh
	# TODO: how can we install vpnkit?

override_dh_installinit:
	# use "docker" as our service name, not "docker-ce"
	dh_installinit --name=docker

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

override_dh_install:
	dh_install
	# TODO Can we do this from within our container?
	dh_apparmor --profile-name=docker-ce -pdocker-ce

%:
	dh $@ --with=bash-completion $(shell command -v dh_systemd_enable > /dev/null 2>&1 && echo --with=systemd)
